import storageApi from '../../core/storage/storage';
import BaseController from '../../core/controllers/Base.controller';
/*eslint-disable */
import casaderData from '!json-loader!./data.json';
/*eslint-enable */

@Inject('$log', '$rootScope', '$scope', '$state', 'globalService', 'staffService', 'ngDialogService')
class AddWorkerInfoController extends BaseController {
  constructor() {
    super();

    this.data = {
      orgId: $rootScope.orgId || storageApi.get('orgId'),
    };
    this.view = {
      files: [],
    };
    const workerId = $state.params.id;
    this.currentIndex = 0;
    this.isTransparency = false;
    this.orgPersonBean = {};
    this.certificateList = [];
    this.orgPersonFileList = [];
    this.queryPostParams = {};
    this.init();
    this.$getWorkerInfo(workerId);
    this.casaderData = casaderData;
  }

  casaderPick(supVal, subVal) {
    this.orgPersonBean.nativePlace = supVal.name + subVal.name;
    this.isCasaderExpanded = false;
  }

  /*eslint-disable */
  $getWorkerInfo(workerId) {
    this.staffService
      .queryRegistrationDetail({ id: workerId })
      .then((res) => {
        this.workerInfo = res.data;
        this.orgPersonBean.departmentCode = this.workerInfo.departmentCode;
        // 根据采集过来的部门查询岗位
        this.$rootScope.deptParams.forEach((item) => {
          if (item.code === this.workerInfo.departmentCode) {
            this.queryPostParams.typeCode = item.typeCode;
          }
        })
        this.queryPostParams.orgId = this.$rootScope.orgId || storageApi.get('orgId');
        this.$getPositionParams(this.queryPostParams);
      });
  }

  // 获得头像src
  $getAvatarSrc() {
    if (this.workerInfo && this.workerInfo.idCardBean
      && this.workerInfo.idCardBean.file
      && this.workerInfo.idCardBean.file.filePath) {
      const src = `/${this.workerInfo.idCardBean.file.filePath}`;
      return src;
    } else {
      return require('../../assets/images/' + 'addBt.jpg');
    }
  }

  $isToggle(item, index) {
    this.$log.debug(item);
    this.currentIndex = index;
    // item.isTransparency = true;
    if (this.currentIndex === 2) {
      this.$checkRequired();
    }
  }

  $toggleNav(type, param) {
    if (type === 1) {
      this.currentIndex = this.currentIndex - 1;
    } else if (type === 2) {
      // // 第二个步骤基本信息必填校验
      // if (param.type.$invalid || param.nativePlace.$invalid || param.cellphone.$invalid || param.department.$invalid
      //   || param.inTime.$invalid ) {
      //   return false;
      // }
      this.currentIndex = this.currentIndex + 1;
    }
    if (this.currentIndex === 2) {
      this.$checkRequired();
    }
    // this.$log.debug(param, param.type, param.type.$invalid);
  }

  $checkRequired() {
    if (this.orgPersonBean && this.orgPersonBean.type) {
      if (this.orgPersonBean.type === '3') {
        this.isSpecial = false;
        this.isThree = true;
        this.isGeneral = false;
      } else if (this.orgPersonBean.type === '2') {
        this.isThree = false;
        this.isSpecial = true;
        this.isGeneral = false;
      } else {
        this.isThree = false;
        this.isSpecial = false;
        this.isGeneral = true;
      }
    }
  }

  // 岗位部门联动
  $isChangeSelect(param) {
    this.departmentJson = JSON.parse(this.department);
    this.orgPersonBean.departmentCode = this.departmentJson.code;
    this.typeCode = this.departmentJson.typeCode;
    const mapParams = {
      orgId: this.data.orgId,
      typeCode: this.typeCode,
    };
    this.$getPositionParams(mapParams);
  }

  // 获取岗位
  $getPositionParams(mapParams) {
    this.globalService
      .queryPost({ mapParams })
      .then((res) => {
        if (res.success) {
          this.positionParams = res.rows;
        }
      });
  }

  // 添加证书
  $addCertify() {
    this.certificateListModel = {
      type: '',
      code: '',
      issueUnit: '',
      issueDate: '',
      validPeriod: '',
    };
    this.certificateList.push(this.certificateListModel);
  }

  $cancelCertify(item, index) {
    this.certificateList.splice(index, 1);
  }

  $finish(param) {
    this.currentFiles = {};
    this.view.files.forEach((item) => {
      this.currentFiles = {
        type: '3',
        fileId: item.id,
      };
      this.orgPersonFileList.push(this.currentFiles);
    });
    this.$log.debug(param);
    const reqData = {
      personCollectBean: this.workerInfo,
      orgPersonBean: this.orgPersonBean,
      certificateList: this.certificateList,
      orgPersonFileList: this.orgPersonFileList,
    };
    this.staffService
      .save(reqData)
      .then((res) => {
        this.$log.debug(res);
        if (res.success) {
          this.ngDialogService.alertSuccess(res.message);
          this.$state.go('workerInfoDetail', { id: res.data.orgPersonBean.id });
        } else {
          this.ngDialogService.alertError(res.message);
        }
      });
  }

  init() {
    this.navData = [
      {
        id: 1,
        name: '采集信息',
        isTransparency: false,
      }, {
        id: 2,
        name: '基本信息',
        isTransparency: false,
      }, {
        id: 3,
        name: '证书信息',
        isTransparency: false,
      }, {
        id: 4,
        name: '附件信息',
        isTransparency: false,
      },
    ];
    this.isThree = false;
    this.isSpecial = false;
    this.isGeneral = true;
  }
}

export default AddWorkerInfoController;
